package cn.edu.cug.cs.gtl.apis.ui.model3duis;

import cn.edu.cug.cs.gtl.api.wrapper.ProjectWrapper;
import cn.edu.cug.cs.gtl.apis.render.Factory;
import cn.edu.cug.cs.gtl.apis.render.Render;
import cn.edu.cug.cs.gtl.apis.render.nodes.Node;
import cn.edu.cug.cs.gtl.apis.render.viewers.Viewer;
import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.UIApp;
import cn.edu.cug.cs.gtl.apis.ui.model3duis.projecttree.ProjectPanel;
import cn.edu.cug.cs.gtl.protos.Layer;
import cn.edu.cug.cs.gtl.protos.Map;
import cn.edu.cug.cs.gtl.protos.Project;

import javax.swing.*;

public class Model3dUIApp extends UIApp {


    protected Project project=null;
    protected Map currentMap=null;
    protected Layer currentLayer=null;

    protected Factory factory = null;

    protected TablePanel tablePanel = null;
    protected ProjectPanel projectPanel = null;
    protected RenderPanel renderPanel = null;

    public Model3dUIApp(AbstractApplication theApp){
        super(theApp);
        this.project = ProjectWrapper.createDefaultProject();
        currentMap = this.project.getMap(0);
        currentLayer = this.currentMap.getLayer(0);
    }

    @Override
    public Type getType() {
        return Type.MODEL3D_UI_APP;
    }


    @Override
    public boolean isValid(){
        return this.factory!=null;
    }



    @Override
    public JComponent initializeUI(){

        initialProjectPanel();
        initialRenderPanel();
        initialTablePanel();

        JSplitPane left = new JSplitPane(JSplitPane.VERTICAL_SPLIT,this.renderPanel,this.tablePanel);
        left.setDividerLocation(800);
        left.setEnabled(true);

        JSplitPane root = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,this.projectPanel);

        root.setDividerLocation(1000);
        root.setEnabled(true);


        return root;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public Map getCurrentMap() {
        return currentMap;
    }

    public void setCurrentMap(Map currentMap) {
        this.currentMap = currentMap;
    }

    public Layer getCurrentLayer() {
        return currentLayer;
    }

    public void setCurrentLayer(Layer currentLayer) {
        this.currentLayer = currentLayer;
    }

    public Factory getFactory() {
        return factory;
    }

    public void setFactory(Factory factory) {
        this.factory = factory;
    }

    public TablePanel getTablePanel() {
        return tablePanel;
    }

    public void setTablePanel(TablePanel tablePanel) {
        this.tablePanel = tablePanel;
    }

    public ProjectPanel getProjectPanel() {
        return projectPanel;
    }

    public void setProjectPanel(ProjectPanel projectPanel) {
        this.projectPanel = projectPanel;
    }

    public RenderPanel getRenderPanel() {
        return renderPanel;
    }

    public void setRenderPanel(RenderPanel renderPanel) {
        this.renderPanel = renderPanel;
    }

    public  void refreshAll(Project newProject){
        if(newProject==null) return;

        this.setProject(newProject);
        this.setCurrentMap(newProject.getMap(0));
        this.setCurrentLayer(newProject.getMap(0).getLayer(0));

        this.getRender().setProject(newProject);

        Node node = this.getRender().createProjectNode(newProject);
        getRenderPanel().reset(node);

        getProjectPanel().reset(newProject);

        getTablePanel().reset();
    }

    public  Render getRender(){
        return this.renderPanel.getRender();
    }

    public  Viewer getViewer(){
        return this.renderPanel.getViewer();
    }


    private void initialRenderPanel(){
        renderPanel = new RenderPanel(theApp);
        String s = null;//"D:" + File.separator + "temp" + File.separator + "test.h3d";
        getRenderPanel().initial(s);
    }

    private void initialTablePanel(){
        tablePanel = new TablePanel(theApp);
        tablePanel.initial();
    }
    private void initialProjectPanel(){
        projectPanel = new ProjectPanel(theApp);
        projectPanel.initial();
    }



}
